home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / deluge / main.py < prev    next >
Text File  |  2009-06-16  |  8KB  |  208 lines

  1. #
  2. # main.py
  3. #
  4. # Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com>
  5. #
  6. # Deluge is free software.
  7. #
  8. # You may redistribute it and/or modify it under the terms of the
  9. # GNU General Public License, as published by the Free Software
  10. # Foundation; either version 3 of the License, or (at your option)
  11. # any later version.
  12. #
  13. # deluge is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. # See the GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with deluge.    If not, write to:
  20. #     The Free Software Foundation, Inc.,
  21. #     51 Franklin Street, Fifth Floor
  22. #     Boston, MA  02110-1301, USA.
  23. #
  24. #    In addition, as a special exception, the copyright holders give
  25. #    permission to link the code of portions of this program with the OpenSSL
  26. #    library.
  27. #    You must obey the GNU General Public License in all respects for all of
  28. #    the code used other than OpenSSL. If you modify file(s) with this
  29. #    exception, you may extend this exception to your version of the file(s),
  30. #    but you are not obligated to do so. If you do not wish to do so, delete
  31. #    this exception statement from your version. If you delete this exception
  32. #    statement from all source files in the program, then also delete it here.
  33. #
  34.  
  35. #
  36.  
  37.  
  38. # The main starting point for the program.    This function is called when the
  39. # user runs the command 'deluge'.
  40.  
  41. """Main starting point for Deluge.  Contains the main() entry point."""
  42.  
  43. import os
  44. import os.path
  45. import sys
  46. from optparse import OptionParser
  47.  
  48. import deluge.log
  49. import deluge.common
  50. import deluge.configmanager
  51.  
  52. def start_ui():
  53.     """Entry point for ui script"""
  54.     import deluge.common
  55.  
  56.     # Setup the argument parser
  57.     parser = OptionParser(usage="%prog [options] [actions]",
  58.                                            version=deluge.common.get_version())
  59.  
  60.     parser.add_option("-u", "--ui", dest="ui",
  61.         help="""The UI that you wish to launch.  The UI choices are:\n
  62.         \t gtk -- A GTK-based graphical user interface (default)\n
  63.         \t web -- A web-based interface (http://localhost:8112)\n
  64.         \t console -- A console or command-line interface""", action="store", type="str")
  65.     parser.add_option("-c", "--config", dest="config",
  66.         help="Set the config folder location", action="store", type="str")
  67.     parser.add_option("-l", "--logfile", dest="logfile",
  68.         help="Output to designated logfile instead of stdout", action="store", type="str")
  69.     parser.add_option("-a", "--args", dest="args",
  70.         help="Arguments to pass to UI, -a '--option args'", action="store", type="str")
  71.     parser.add_option("-L", "--loglevel", dest="loglevel",
  72.         help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str")
  73.     parser.add_option("-q", "--quiet", dest="quiet",
  74.         help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False)
  75.     parser.add_option("-s", "--set-default-ui", dest="default_ui",
  76.         help="Sets the default UI to be run when no UI is specified", action="store", type="str")
  77.  
  78.     # Get the options and args from the OptionParser
  79.     (options, args) = parser.parse_args()
  80.  
  81.     if options.config:
  82.         if not os.path.exists(options.config):
  83.             # Try to create the config folder if it doesn't exist
  84.             try:
  85.                 os.makedirs(options.config)
  86.             except Exception, e:
  87.                 pass
  88.         elif not os.path.isdir(options.config):
  89.             print "Config option needs to be a directory!"
  90.             sys.exit(1)
  91.  
  92.     if options.default_ui:
  93.         if options.config:
  94.             deluge.configmanager.set_config_dir(options.config)
  95.  
  96.         config = deluge.configmanager.ConfigManager("ui.conf")
  97.         config["default_ui"] = options.default_ui
  98.         config.save()
  99.         print "The default UI has been changed to", options.default_ui
  100.         sys.exit(0)
  101.  
  102.     if options.quiet:
  103.         options.loglevel = "none"
  104.  
  105.     else:
  106.         if not os.path.exists(deluge.common.get_default_config_dir()):
  107.             os.makedirs(deluge.common.get_default_config_dir())
  108.  
  109.     # Setup the logger
  110.     deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
  111.  
  112.     version = deluge.common.get_version()
  113.     if deluge.common.get_revision() != "":
  114.         version = version + "r" + deluge.common.get_revision()
  115.  
  116.     from deluge.log import LOG as log
  117.     log.info("Deluge ui %s", version)
  118.     log.debug("options: %s", options)
  119.     log.debug("args: %s", args)
  120.     log.debug("ui_args: %s", args)
  121.  
  122.     from deluge.ui.ui import UI
  123.     log.info("Starting ui..")
  124.     UI(options, args, options.args)
  125.  
  126. def start_daemon():
  127.     """Entry point for daemon script"""
  128.     import deluge.common
  129.  
  130.     # Setup the argument parser
  131.     parser = OptionParser(usage="%prog [options] [actions]",
  132.                                            version=deluge.common.get_version())
  133.     parser.add_option("-p", "--port", dest="port",
  134.         help="Port daemon will listen on", action="store", type="int")
  135.     parser.add_option("-d", "--do-not-daemonize", dest="donot",
  136.         help="Do not daemonize", action="store_true", default=False)
  137.     parser.add_option("-c", "--config", dest="config",
  138.         help="Set the config location", action="store", type="str")
  139.     parser.add_option("-l", "--logfile", dest="logfile",
  140.         help="Set the logfile location", action="store", type="str")
  141.     parser.add_option("-P", "--pidfile", dest="pidfile",
  142.         help="Use pidfile to store process id", action="store", type="str")
  143.     parser.add_option("-L", "--loglevel", dest="loglevel",
  144.         help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str")
  145.     parser.add_option("-q", "--quiet", dest="quiet",
  146.         help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False)
  147.  
  148.     # Get the options and args from the OptionParser
  149.     (options, args) = parser.parse_args()
  150.  
  151.     if options.quiet:
  152.         options.loglevel = "none"
  153.  
  154.     if options.config:
  155.         if not os.path.exists(options.config):
  156.             # Try to create the config folder if it doesn't exist
  157.             try:
  158.                 os.makedirs(options.config)
  159.             except Exception, e:
  160.                 pass
  161.     else:
  162.         if not os.path.exists(deluge.common.get_default_config_dir()):
  163.             os.makedirs(deluge.common.get_default_config_dir())
  164.  
  165.     # Sets the options.logfile to point to the default location
  166.     def open_logfile():
  167.         if not options.logfile:
  168.             if options.config:
  169.                 options.logfile = os.path.join(options.config, "deluged.log")
  170.             else:
  171.                 config_dir = deluge.common.get_default_config_dir()
  172.                 options.logfile = os.path.join(config_dir, "deluged.log")
  173.  
  174.     # Writes out a pidfile if necessary
  175.     def write_pidfile():
  176.         if options.pidfile:
  177.             open(options.pidfile, "wb").write("%s\n" % os.getpid())
  178.  
  179.     # If the donot daemonize is set, then we just skip the forking
  180.     if not options.donot:
  181.         # Windows check, we log to the config folder by default
  182.         if deluge.common.windows_check() or deluge.common.osx_check():
  183.             open_logfile()
  184.             write_pidfile()
  185.         else:
  186.             if os.fork() == 0:
  187.                 os.setsid()
  188.                 if os.fork() == 0:
  189.                     open_logfile()
  190.                     write_pidfile()
  191.                 else:
  192.                     os._exit(0)
  193.             else:
  194.                 os._exit(0)
  195.     else:
  196.         # Do not daemonize
  197.         write_pidfile()
  198.  
  199.     # Setup the logger
  200.     deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
  201.  
  202.     try:
  203.         from deluge.core.daemon import Daemon
  204.         Daemon(options, args)
  205.     except Exception, e:
  206.         from deluge.log import LOG as log
  207.         log.exception(e)
  208.